import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';
import router from 'umi/router';
import { fetchValidation } from '@/utils';
import {
  queryCabinePlansDetail,
  queryGardenList,
  queryOperationsList,
  queryDepositCabList,
  operAddOrUpdateSetting,
  queryslaveTypeList,
  checkPayMask,
  opertionAddOrUpdate,
  opertionDeleteSlave,
} from './service';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetch: Effect;
    fetchGarden: Effect;
    fetchOperations: Effect;
    fetchDepositCab: Effect;
    fetchSlaveTypeList: Effect;
    checkPayMask: Effect;
    addOrupdate: Effect;
    addOrupdateCab: Effect;
    deleteSlave: Effect;
  };
  reducers: {
    save: Reducer;
    saveGarden: Reducer;
    saveDepositCab: Reducer;
    saveslaveType: Reducer;
    savePayMask: Reducer;
    clear: Reducer;
  };
}

const stateInfo = {
  detailInfo: {},
  gardenInfo: {
    count: 0,
    list: [],
  },
  DepositCabList: [],
  slaveTypeList: [],
  isPayMask: true,
};

const Model: ModelType = {
  namespace: 'cabinetManagementAdd',

  state: stateInfo,

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(queryCabinePlansDetail, payload);
      yield put({
        type: 'save',
        payload: response.body,
      });
    },

    * fetchGarden({ payload }, { call, put }) {
      const response = yield call(queryGardenList, payload);
      yield put({
        type: 'saveGarden',
        payload: response.body,
      });
    },

    * fetchOperations({ payload }, { call, put }) {
      const response = yield call(queryOperationsList, payload);
      yield put({
        type: 'saveGarden',
        payload: response.body,
      });
    },

    * fetchDepositCab({ payload }, { call, put }) {
      const response = yield call(queryDepositCabList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'saveDepositCab',
        payload: response.body || [],
      });
    },

    * fetchSlaveTypeList({ payload }, { call, put }) {
      const response = yield call(queryslaveTypeList, payload);
      const data = fetchValidation(response);
      if (data) {
        const { body } = data;
        const result = Object.keys(body).map((item: any) => ({
          key: item,
          value: body[item],
        }));
        yield put({
          type: 'saveslaveType',
          payload: result || [],
        });
      }
    },

    * checkPayMask({ payload }, { call, put }) {
      const response = yield call(checkPayMask, payload);
      const data = fetchValidation(response);
      if (data) {
        if (!data.body) message.warning('您选择的套餐为免费套餐，付费方不可设置');
        yield put({
          type: 'savePayMask',
          payload: data.body,
        });
      }
    },

    * addOrupdate({ payload }, { call, put }) { // 新增与修改柜体寄存配置
      const response = yield call(operAddOrUpdateSetting, payload);
      const data = fetchValidation(response);
      if (data) message.success('配置成功');
    },

    * addOrupdateCab({ payload }, { call, put }) { // 新增与修改柜体
      const response = yield call(opertionAddOrUpdate, payload);
      const data = fetchValidation(response);
      if (data) {
        message.success(payload.id ? '修改成功' : '添加成功');
        yield put({
          type: 'global/setPath',
        });
        router.goBack();
      }
    },

    * deleteSlave({ payload }, { call, put }) { // 删除副柜
      const response = yield call(opertionDeleteSlave, payload);
      const data = fetchValidation(response);
      if (data) {
        message.success('删除成功');
        yield put({
            type: 'global/setPath',
        });
        router.goBack();
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        detailInfo: action.payload,
      };
    },

    saveGarden(state, action) {
      return {
        ...state,
        gardenInfo: action.payload,
      };
    },

    saveDepositCab(state, action) {
      return {
        ...state,
        DepositCabList: action.payload,
      };
    },

    saveslaveType(state, action) {
      return {
        ...state,
        slaveTypeList: action.payload,
      };
    },

    savePayMask(state, { payload }) {
      return {
        ...state,
        isPayMask: payload,
      };
    },

    clear() {
      return stateInfo;
    },
  },
};

export default Model;
